<template>
  <div class="box">
    <div class="Login_box">
      <!--头像区-->
      <div class="avatar_box">
        <img src="https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg">
      </div>
      <!--登录表单区-->
      <div class="form">
        <el-form style="width: 60%;" :model="loginForm" :rules="rules" ref="loginFormRef">
          <!--用户名-->
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" prefix-icon="el-icon-user"></el-input>
          </el-form-item>
          <!--密码-->
          <el-form-item prop="password">
            <el-input type="password" v-model="loginForm.password" prefix-icon="el-icon-lock"></el-input>
          </el-form-item>
          <!--按钮-->
          <el-form-item class="bth">
            <el-button type="primary" @click="Login">登录</el-button>
            <el-button type="info" @click="onResetting">重置</el-button>
          </el-form-item>

        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      //这是登录表单的绑定的数据对象
      loginForm: {
        username: '',
        password: ''
      },
      //这是表单验证的相关规则
      rules: {
        username: [
          {required: true, message: '请输入登录名称', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入登录密码', trigger: 'blur'},
          {min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    onResetting() {
      //这个是官方提供的重置表单的api   会更加完善一点  所以就用官方的
      this.$refs.loginFormRef.resetFields();
    },
    Login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return
        //这里的async  和   await  是为了将promise对象  变成一个正常的对象   所以使用这个就相当于  不用使用then了 算是简化了
        const {data} = await this.$http.post('login', this.loginForm)
        if(data.meta.status!==200)
        {
          this.$message.error(`${data.meta.msg}`);
          return
        }else {
          window.sessionStorage.setItem('token',data.data.token)
          this.$message({
            message: `${data.meta.msg}`,
            type: 'success'
          });
          await this.$router.push('/home')
        }

      })
    }
  }
}
</script>

<style lang="less" scoped>
.box {
  height: 100%;
  background-color: #2b4b6b;
  display: flex;
  justify-content: center;
  align-items: center;

  .Login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;

    .avatar_box {
      height: 130px;
      width: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      margin-top: -65px;
      background-color: #fff;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }

    .form {
      width: 100%;
      height: 65%;
      display: flex;
      justify-content: center;
      align-items: center;

      .bth {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
